//
//  NewsViewController.m
//  iMADE
//
//  Created by Ed Knutson on 10/20/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "NewsViewController.h"

#import "Constants.h"
#import "NewsItem.h"
#import "WebViewController.h"

@implementation NewsViewController

@synthesize newsArray, newsCell;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)processItems {
	NSLog(@"processing items, count == %d", [self.items count]);
	NSMutableArray *news = [[NSMutableArray alloc] initWithCapacity:[self.items count]];
	for(int i=0; i<[self.items count]; i++) {
		NewsItem *newsItem = [[NewsItem alloc] initWithDictionary:[self.items objectAtIndex:i]];
		[news addObject:newsItem];
		[newsItem release];
	}
	self.newsArray = news;
	[news release];
	[super processItems];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.prefix = [FEED_PREFIX stringByAppendingString:NEWS_PREFIX];
	
	self.title = @"News";
	if(self.feedUrl == nil)
		self.feedUrl = NEWS_FEED_URL;
	self.statusMsg = @"No news is good news";
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	int count = [newsArray count];
	if(count == 0)
		return 1;
	return [newsArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *GenericCellIdentifier = @"Cell";
    static NSString *NewsCellIdentifier = @"NewsCell";
	static NSDateFormatter *dateFormat = nil;
	if(dateFormat == nil) {
		dateFormat = [[NSDateFormatter alloc] init];
		[dateFormat setDateStyle:NSDateFormatterLongStyle];
		[dateFormat setTimeStyle:NSDateFormatterNoStyle];
	}

    UITableViewCell *cell = nil;
	
    if([newsArray count] == 0) {
		cell = [tableView dequeueReusableCellWithIdentifier:GenericCellIdentifier];
		if (cell == nil) {			
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GenericCellIdentifier] autorelease];
		}
		
		cell.textLabel.text = statusMsg;
	} else {
		cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifier];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NewsCellIdentifier] autorelease];
		}
    
		// Set up the cell...
		NewsItem *newsItem = [newsArray objectAtIndex:indexPath.row];
		cell.textLabel.text = newsItem.title;
		cell.detailTextLabel.text = [dateFormat stringFromDate:newsItem.publishDate];
	}
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	if([newsArray count] == 0)
		return;
	WebViewController *anotherViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
	NewsItem *news = [newsArray objectAtIndex:indexPath.row];
	anotherViewController.url = news.link;
	[self.navigationController pushViewController:anotherViewController animated:YES];
	
}


- (void)dealloc {
	[newsArray release];
	[newsCell release];
    [super dealloc];
}


@end

